package com.ball.dev.controller;


import cn.hutool.core.lang.UUID;
import com.ball.common.utils.IFileStorage;
import com.ball.dev.entity.po.BallButton;
import com.ball.dev.service.IBallButtonService;
import com.ball.dev.service.impl.BallButtonServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

/**
 * url
 */
@RestController
public class UrlController {
    @Autowired
    private IFileStorage ossUtil;

    @Autowired
    private BallButtonServiceImpl ballButtonService;

    @GetMapping("/url")
    public void url(String folderPath) throws Exception {

//        String folderPath = "/Users/lou/日常文件/毕业设计/ball/资源"; // 指定文件夹路径

        try (Stream<Path> paths = Files.walk(Paths.get(folderPath))) {
            paths.filter(Files::isRegularFile).forEach(path -> {
                File file = path.toFile();
                try (FileInputStream inputStream = new FileInputStream(file)) {

//                    String fileName = generateNewFileName(file.getName());
                    String fileName = file.getName();

                    String result = ossUtil.uploadFile(fileName,inputStream);
                    System.out.println(result);

                    String[] strings = fileName.split("\\.");

                    BallButton button = new BallButton();
                    button.setNumber(strings[0]);
                    button.setPhoto(result);
                    ballButtonService.addBallButton1(button);




                } catch (IOException e) {
                    e.printStackTrace();
                }

            });

        }

    }




    private String generateNewFileName(String originalFilename) {

        // 1.获取后缀
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        // 2.生成新文件名
        return UUID.randomUUID().toString(true) + "." + suffix;
    }
}
